﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace University.Workshop.DataAccess
{
    public class DataAccessFineCollectControl
    {
        public static ICollection<Model.FineCollectControl> GetBetweenDates(DateTime initialDate, DateTime endDate) 
        {
            WFPEntities.Instance.DiscardChanges();
            return (from c in WFPEntities.Instance.FineCollectControl
                    where c.CollectPayDate >= initialDate && c.CollectPayDate <= endDate && c.IsPaid
                    select c).ToList();
        }

        public static ICollection<Model.FineCollectControl> SearchByClientId(Model.CollectControl chargetoFind)
        {
            WFPEntities.Instance.DiscardChanges();
            return (from c in WFPEntities.Instance.FineCollectControl
                    where c.KardexId == chargetoFind.KardexId && c.IsPaid == false
                    select c).ToList();
        }

        public static void UpdateCollect(Model.CollectControl collectToUpdate)
        {
            WFPEntities.Instance.DiscardChanges();

            Model.FineCollectControl cc = (from wp in WFPEntities.Instance.FineCollectControl
                                            where wp.CollectId == collectToUpdate.CollectId
                                            select wp).First();

            cc.CollectPayDate = collectToUpdate.CollectPayDate;
            cc.IsPaid = true;

            WFPEntities.Instance.SaveChanges();
        }

        public static void RegisterFineCollectControl(Model.FineCollectControl otherCollect)
        {
            WFPEntities.Instance.DiscardChanges();
            WFPEntities.Instance.FineCollectControl.Add(otherCollect);
            WFPEntities.Instance.SaveChanges();
        }

        public static bool ExistAnyFineCollectWithThisKardexIDAndDate (int kardexID, DateTime date)
        {
            var collects = (from fc in WFPEntities.Instance.FineCollectControl
                            where fc.KardexId == kardexID && fc.ControlDate.Month == date.Month && fc.ControlDate.Year == date.Year
                            select fc).ToList ();

            return (collects.Count > 0 ? true : false);
        }

    }
}
